//
//  VistaPerfil.m
//  ProyectoMoviTiendas
//
//  Created by Mario Franco on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VistaPerfil.h"
#import "Tienda.h"
#import "Carrito.h"
#import "VistaItems.h"


@interface VistaPerfil ()
{
    Carrito * carritoSeleccionado;
    
}
@end

@implementation VistaPerfil
@synthesize lblNombre;
@synthesize lblDireccion;
@synthesize lblBarrio;
@synthesize principal;
@synthesize historial;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    lblNombre.text = principal.cliente.nombre;
    lblDireccion.text = principal.cliente.direccion1;
    lblBarrio.text = principal.cliente.barrio.nombre;
    [self.tableView reloadData];    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.hidesBackButton = YES;
    principal = [MoviTiendas darInstancia];
    historial = principal.cliente.historial;
    lblNombre.text = principal.cliente.nombre;
    lblDireccion.text = principal.cliente.direccion1;
    lblBarrio.text = principal.cliente.barrio.nombre;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setLblNombre:nil];
    [self setLblDireccion:nil];
    [self setLblBarrio:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [historial count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    
    int posMax = [historial count];
    Carrito * carrito = [historial objectAtIndex:(posMax-indexPath.row-1)];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM/dd/yyyy HH:mm"];
    
    NSDate *now = carrito.fecha;
    
    NSString *dateString = [format stringFromDate:now];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",carrito.tienda.nombre, dateString];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int posMax = [historial count];
    carritoSeleccionado = [historial objectAtIndex:posMax- indexPath.row-1];
    
    [self performSegueWithIdentifier:@"verItemsCarrito" sender:self];


}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"verItemsCarrito"]) 
    {
        VistaItems * vista = [segue destinationViewController];
        vista.items = carritoSeleccionado.items;
        
    }

}

@end
